package net.jumpengine.bean;

import javafx.scene.canvas.GraphicsContext;
import net.jumpengine.Manager;
import net.jumpengine.enume.TextAlign;
import net.jumpengine.po.Border;
import net.jumpengine.util.UDraw;

/**
 * 简单的带背景边框的文字显示, 文字太长会换行.
 * <br /><br />
 * <b>作者:&nbsp;</b>张天文 <br />
 * <b>日期:&nbsp;</b>2020年4月4日 下午9:07:45<br />
 * @date
 */
public class TextRect extends TextBase {
	protected Border border;
	protected TextAlign textAlign=TextAlign.center;
	
	private int r,cx,cy,cwidth,cheight;
	
	public TextRect(int x, int y, int width,int height, String text) {
		super(x, y, width, text);
		this.height=height;
		border=new Border();
	}
	
	public TextRect(int x, int y, int width,int height, String text, Border border) {
		super(x, y, width, text);
		this.height=height;
		this.border=border;
	}

	@Override
	public void draw(GraphicsContext g) {
		if(!visible)return;
		r=(border==null?0:border.getRadius());
		g.setStroke(border.getColor());
		if(r==0) {
			g.strokeRect(x, y, width, height);
		}else {
			g.strokeRoundRect(x, y, width, height, r, r);
		}
		
		if(change) {
			cx=x+(border==null?0:border.getLeft());
			cy=y+(border==null?0:border.getTop());
			cwidth= width-(border==null?0:border.getRight())-(border==null?0:border.getLeft());
			cheight=height-(border==null?0:border.getBottom())-(border==null?0:border.getTop());
			list=UDraw.splitString(text, width, font);
		}
		
		g.setFill(backColor!=null?backColor:Manager.getBackColor());
		if(r==0) {
			g.fillRect(cx,cy,cwidth,cheight);
		}else {
			g.fillRoundRect(cx,cy, cwidth, cheight, r, r);
		}
		
		g.setFill(color);
		g.setFont(font);
		UDraw.drawCenterStrings(g, list, cx, cy, cwidth, cheight, rowSpacing);
		change=false;
	}
}
